package utils.collection;

import java.util.*;

/**
 * Helper methods for handling collections
 */
public class CollectionUtils {
	
	/**
	 * Select items from collection that matches to the given condition.
	 * 
	 * More info:
	 * http://stackoverflow.com/questions/122105/java-what-is-the-best-way-to-filter-a-collection/122207#122207
	 * 
	 * @param <T>
	 * @param target Collection to be filtered
	 * @param predicate Predicate that contains the test
	 * @return Filtered collection
	 */
	public static <T> Collection<T> select(Collection<T> target, Predicate<T> predicate) {
	    Collection<T> result = new ArrayList<T>();
	    for (T element: target) {
	        if (predicate.apply(element)) {
	            result.add(element);
	        }
	    }
	    return result;
	}
}
